package com.example.mobile2021606134chhnewdemo.WeatherModule.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.mobile2021606134chhnewdemo.R;
import com.example.mobile2021606134chhnewdemo.WeatherModule.Http;
import com.example.mobile2021606134chhnewdemo.WeatherModule.SGonAir;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class AirFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    TextView tvco,tvno2,tvpm10,tvso2,tvplaceName;
    OkHttpClient okHttpClient = new OkHttpClient( );

    public AirFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AirFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AirFragment newInstance(String param1, String param2) {
        AirFragment fragment = new AirFragment( );
        Bundle args = new Bundle( );
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments( ) != null) {
            mParam1 = getArguments( ).getString(ARG_PARAM1);
            mParam2 = getArguments( ).getString(ARG_PARAM2);


        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
      View view=inflater.inflate(R.layout.fragment_air, container, false);
      in(view);
      new Thread(new Runnable( ) {
          @Override
          public void run() {
              RequestBody requestBody = new FormBody.Builder( ).add("cityId", "917")
                      .add("token", Http.AQITOKEN)
                      .build( );

              Request request = new Request.Builder( )
                      .addHeader("Authorization", "APPCODE " + Http.APPCODE)
                      .url(Http.AQIURL)
                      .post(requestBody)
                      .build( );
              Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback( ) {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                     String result=response.body().string();
                Log.i("ZX", result);
                getActivity().runOnUiThread(new Runnable( ) {
                    @Override
                    public void run() {
                        Gson gson = new Gson( );
                        SGonAir sGonAir = gson.fromJson(result, SGonAir.class);
                        String co = sGonAir.getData( ).getAqi( ).getCo( );
                        String place = sGonAir.getData( ).getCity( ).getName( );
                        String so2 = sGonAir.getData( ).getAqi( ).getSo2( );
                        String no2 = sGonAir.getData( ).getAqi( ).getNo2( );
                        String pm10 = sGonAir.getData( ).getAqi( ).getPm10( );
                        tvco.setText("CO:  "+co+"μg/m3");
                        tvno2.setText("NO2:  "+no2+"μg/m3");
                        tvplaceName.setText("城市："+place);
                        tvpm10.setText("pm10:  "+pm10);
                        tvso2.setText("SO2:  "+so2+"μg/m3");
                    }
                });
            }
        });
          }
      }).start();

        return view;
    }
    public void in(View view){
        tvco = view.findViewById(R.id.tvAirCO);
        tvno2 = view.findViewById(R.id.tvAirNO2);
        tvplaceName = view.findViewById(R.id.tvAirPlace);
        tvpm10 = view.findViewById(R.id.tvAirPM10);
        tvso2 = view.findViewById(R.id.tvAirSO2);
    }

}